﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

using System.Security;
using webtracuudiem.Truyxuatdulieu;
using System.Web.Security;
using System.Data.OleDb;
using System.Data.Common;

namespace webtracuudiem
{
    public partial class Tracuudiem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sqlSelect = "select MaSV,MaLop,HoLot,Ten,convert(varchar(50),NgaySinh,103) from SinhVien where MaSV=@masv";
            if (Page.User.Identity.IsAuthenticated == true)
            {
                lblMaSV.Text = Page.User.Identity.Name;
                DataTable tbl = TienIchSql.ChayLenhSelectDuLieu(sqlSelect, "@MaSV", lblMaSV.Text);
                lblHoten.Text = tbl.Rows[0].ItemArray[2].ToString() + " " + tbl.Rows[0].ItemArray[3].ToString();
                lblNgaysinh.Text = tbl.Rows[0].ItemArray[4].ToString();
                lblLop.Text = tbl.Rows[0].ItemArray[1].ToString();

            }        
           
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            
           
            
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
        {

        }

        protected void ButXemdiem_Click(object sender, EventArgs e)
        {
           
            if (int.Parse(DropHocky.SelectedValue) == 1)
            {
                GridView1.Enabled = true;
                GridView1.Visible = true;
                GridView2.Enabled = false;
                GridView2.Visible = false;
            }
            if (int.Parse(DropHocky.SelectedValue) == 2)
            {
                GridView1.Enabled = true;
                GridView1.Visible = true;
                GridView2.Enabled = false;
                GridView2.Visible = false;
            }
            if (int.Parse(DropHocky.SelectedValue) == 3)
            {
                GridView1.Enabled = true;
                GridView1.Visible = true;
                GridView2.Enabled = false;
                GridView2.Visible = false;
            }
            if (int.Parse(DropHocky.SelectedValue) == 4)
            {
                GridView1.Enabled = true;
                GridView1.Visible = true;
                GridView2.Enabled = false;
                GridView2.Visible = false;
            }
            ///////////////////////////////////////////
            string masv = lblMaSV.Text;
            string hocky = DropHocky.SelectedValue;
            string sqlSelect = "select round(AVG(DiemKQ),1) as 'dtb' from Diem d,MonHocCuaNganh mn where d.MaMH=mn.MaMH and HocKy=@hocky AND MaSV=@masv group by MaSV";
            DataTable tbl = TienIchSql.ChayLenhSelectDuLieu(sqlSelect, "@hocky", hocky, "@MaSV", masv);
            //TextBoxdtbhk.Text = tbl.Rows[0].ItemArray[0].ToString();
            ////////////////////////////////////////////
        }
        double tongDiem = 0;
        double soMon = 0;
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;
            if (row.RowType == DataControlRowType.Footer)
            {
                row.Cells[5].Text = (tongDiem / soMon).ToString();
            }
            if (row.RowType != DataControlRowType.DataRow)
                return;
            
            string diemKqCell = row.Cells[5].Text;
            double diemKQ ;

            if (double.TryParse(diemKqCell, out diemKQ) == false)
                diemKQ = 0;
            tongDiem += diemKQ;
            soMon++;
        }

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;
            if (row.RowType == DataControlRowType.Footer)
            {
                row.Cells[5].Text = (tongDiem / soMon).ToString();
            }
            if (row.RowType != DataControlRowType.DataRow)
                return;

            string diemKqCell = row.Cells[5].Text;
            double diemKQ;

            if (double.TryParse(diemKqCell, out diemKQ) == false)
                diemKQ = 0;
            tongDiem += diemKQ;
            soMon++;
        }

        protected void ButXemdiemtoankhoa_Click(object sender, EventArgs e)
        {
            GridView2.Enabled = true;
            GridView2.Visible = true;
            GridView1.Enabled = false;
            GridView1.Visible = false;
        }

        protected void GridView1_SelectedIndexChanged2(object sender, EventArgs e)
        {

        }

        protected void ButThoat_Click(object sender, EventArgs e)
        {
           
            FormsAuthentication.SignOut();
            Response.Redirect("/Default.aspx");
        
        }
    }
}